package cn.com.zetatech.loader.util;

import cn.com.zetatech.loader.entity.DieBO;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BinMapUtils {
    public static void main(String[] args) {
        // 创建一些示例的 DieBO 对象
        List<DieBO> bins = new ArrayList<>();
        bins.add(new DieBO(1, 1, "#FF0000", "1", "2")); // 红色Bin
        bins.add(new DieBO(1, 2, "#00FF00", "1", "2")); // 绿色Bin
        bins.add(new DieBO(2, 3, "#0000FF", "1", "2")); // 蓝色Bin
        bins.add(new DieBO(1, 4, "#FFFF00", "1", "2")); // 黄色Bin
        bins.add(new DieBO(1, 5, "#FF00FF", "1", "2")); // 紫色Bin

        // 使用 drawBinImage 方法生成图像
        BufferedImage image = drawBinImage(bins);

        try {
            // 将生成的图像保存为PNG文件
            File outputfile = new File("bin_image.png");
            ImageIO.write(image, "png", outputfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //    /**
//     * binmap绘制
//     * @param bins
//     * @return
//     */
//    public static BufferedImage drawBinImage(List<DieBO> bins) {
//        // 设置图片大小
//        int imageSize = 500;
//        // 设置圆边框线宽
//        int lineWidth = 2;
//        int mapWidth = imageSize + lineWidth;
//        int mapHeight = imageSize + lineWidth;
//
//        // 计算X轴和Y轴上的die个数
//        int maxDieX = bins.stream().mapToInt(DieBO::getDieX).map(Math::abs).max().orElse(1);
//        int maxDieY = bins.stream().mapToInt(DieBO::getDieY).map(Math::abs).max().orElse(1);
//        int dieCountX = maxDieX * 2;
//        int dieCountY = maxDieY * 2;
//
//        // 计算每个die的宽度和高度
//        float dieSizeX = mapWidth / dieCountX;
//        float dieSizeY = mapHeight / dieCountY;
//
//        BufferedImage b = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_4BYTE_ABGR);
//        Graphics2D g = b.createGraphics();
//        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//
//
//        // 画Bin
//        for (DieBO bin : bins) {
//            g.setColor(Color.decode(bin.getColor()));
//
//            // 计算实际的X和Y轴坐标比例
//            float xRatio = (float) bin.getDieX() / maxDieX;
//            float yRatio = (float) bin.getDieY() / maxDieY;
//
//            // 将比例转换为图像坐标
//            float binX =  xRatio * (mapWidth - lineWidth) / 2;
//            float binY =  yRatio * (mapHeight - lineWidth) / 2;
//            // 绘制Bin为矩形
//            g.fillRect((int) (binX - dieSizeX ), (int) (binY-dieSizeY), (int) dieSizeX, (int) dieSizeY);
//        }
//
//        g.dispose();
//        return b;
//    }
    public static BufferedImage drawBinImage(List<DieBO> bins) {
        // 设置图片大小
        int imageSize = 500;
        // 设置圆边框线宽
        int lineWidth = 2;
        int mapWidth = imageSize + lineWidth;
        int mapHeight = imageSize + lineWidth;

        // 计算X轴和Y轴上的die个数
        int minDieX = bins.stream().mapToInt(DieBO::getDieX).min().orElse(0);
        int maxDieX = bins.stream().mapToInt(DieBO::getDieX).max().orElse(1);
        int minDieY = bins.stream().mapToInt(DieBO::getDieY).min().orElse(0);
        int maxDieY = bins.stream().mapToInt(DieBO::getDieY).max().orElse(1);

        int dieCountX = maxDieX - minDieX + 1;
        int dieCountY = maxDieY - minDieY + 1;

        // 计算每个die的宽度和高度
        float dieSizeX = (float) mapWidth / dieCountX;
        float dieSizeY = (float) mapHeight / dieCountY;

        BufferedImage b = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = b.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 画Bin
        for (DieBO bin : bins) {
            g.setColor(Color.decode(bin.getColor()));

            // 计算实际的X和Y轴坐标
            float binX = (bin.getDieX() - minDieX) * dieSizeX;
            float binY = (bin.getDieY() - minDieY) * dieSizeY;

            // 绘制Bin为矩形
            g.fillRect((int) binX, (int) binY, (int) dieSizeX, (int) dieSizeY);
        }

        g.dispose();
        return b;
    }


}
